package cn.edu.gcu.giftems.daos;

import cn.edu.gcu.giftems.pojos.Order;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import java.util.List;

/**
 * 订单Dao类.
 *
 * @author ZhengHQ
 * @create 2018-09-24
 * @see
 */
public class OrderDao extends HibernateDaoSupport {
    
    //获取订单总记录
    public int findCount() {
        String hql = "select count(*) from Order";
        //List list=new ArrayList();
        List<Long> list = (List<Long>)this.getHibernateTemplate().find(hql);
        if(list.size() > 0){
            return list.get(0).intValue();
        }
        return 0;
    }
    //获取订单集合信息
    public List<Order> findByPage(int begin, int pageSize) {
        try {
            String hql =  "select new cn.edu.gcu.giftems.pojos.Order(o.id, o.orderDate, o.status, u.id, u.name )" +
                    "from Order o, User u where o.user = u.id order by o.id";
            List<Order> list = (List<Order>)this.getHibernateTemplate().execute(
                    new HibernateCallback<Object>() {
                        public Object doInHibernate(Session arg0)
                                throws HibernateException {
                            Query query = arg0.createQuery(hql);
                            query.setFirstResult(begin);
                            query.setMaxResults(pageSize);
                            return query.list();
                        }
                    }
            );
            return list;
        }catch (RuntimeException re) {
            throw re;
        }
    }
    //添加订单
    public void save(Order order) {
        this.getHibernateTemplate().save(order);
    }
    
    //根据订单编号查询订单信息
    public Order finById(int id) {
        return this.getHibernateTemplate().get(Order.class,id);
    }
    
    //更新订单信息
    public void update(Order order) {
        this.getHibernateTemplate().update(order);
    }
    
    //根据订单id删除订单
    public void delete(Order order) {
        this.getHibernateTemplate().delete(order);
    }

    public List<Order> findAll() {
        String hql = "from cn.edu.gcu.giftems.pojos.Order";
        return (List<Order>)this.getHibernateTemplate().find(hql);
    }

    public List<Order> findByParameter(String filter_name, String simple_filter_value) {
        List<Order> list = null;
        String hql =  "select new cn.edu.gcu.giftems.pojos.Order(o.id, o.orderDate, o.status, u.id, u.name )" +
                "from Order o, User u where o." + filter_name + " like '%" + simple_filter_value + "%'" +
                " and o.user = u.id order by o.id";
        try {
            list = (List<Order>) this.getHibernateTemplate().execute(
                    new HibernateCallback<Object>() {
                        public Object doInHibernate(Session arg0)
                                throws HibernateException {
                            Query query = arg0.createQuery(hql);
                            return query.list();
                        }
                    }
            );
        } catch (RuntimeException re) {
            throw re;
        }
        return list;
    }

    public List<Order> findByTime( String begin_time, String finish_time) {
        List<Order> list = null;
        String hql =  "select new cn.edu.gcu.giftems.pojos.Order(o.id, o.orderDate, o.status, u.id, u.name )" +
                "from Order o, User u where o.orderDate > '" + begin_time + "' and o.orderDate < '" + finish_time +
                "' and o.user = u.id order by o.id";
        try {
            list = (List<Order>) this.getHibernateTemplate().execute(
                    new HibernateCallback<Object>() {
                        public Object doInHibernate(Session arg0)
                                throws HibernateException {
                            Query query = arg0.createQuery(hql);
                            return query.list();
                        }
                    }
            );
        } catch (RuntimeException re) {
            throw re;
        }
        return list;
    }
}
